package com.zkingsoft.actions.superaction;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.config.Dictionary;
import com.zkingsoft.model.Company;
import com.zkingsoft.model.PaginationVO;
import com.zkingsoft.model.Users;
import com.zkingsoft.plugin.util.EncrypUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.CompanyService;
import com.zkingsoft.services.UsersService;

/**
 * @project MatrixCms
 * @package com.zkingsoft.actions.superaction
 * @class SuperController.java
 * @author Matrix-J E-mail:<935090232@qq.com>
 * @date 2016年4月21日10:31
 * @description 企业管理员
 */
@Controller
@RequestMapping(value = "super")
public class SuperController extends BaseController {

	@Resource
	private UsersService currentService;
	@Resource
	private CompanyService companyService;

	/**
	 * 页面定向方法，每个权限模块公用一个，每个模块共享一个一级路径，已便于进行权限过滤
	 * 
	 * @param page
	 * @return
	 */
	@RequestMapping(value = "/redirect/{page1}/{page2}")
	public String redirect(@PathVariable("page1") String page1, @PathVariable("page2") String page2) {
		return "super/" + page1 + "/" + page2;
	}

	@RequestMapping(value = "/redirect/{page1}")
	public String redirect(@PathVariable("page1") String page1) {
		return "super/" + page1;
	}

	@RequestMapping(value = "/showList")
	public @ResponseBody AjaxResult showList(HttpServletRequest request, Users admin, PaginationVO pageVo) {
		admin.setValid(1);
		admin.setUserType(Dictionary.USER_TYPE_ADMIN);
		return showList(currentService, admin, pageVo);
	}

	@RequestMapping(value = "/addOrModify")
	public @ResponseBody AjaxResult addOrModify(Users admin) {
		// 判断去重
		if (currentService.checkIsAccountRepeat(admin)) {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "操作失败，账号已经存在！");
		} else if (currentService.checkIsEmailRepeat(admin)) {
			return new AjaxResult(AjaxResult.STATUS_ERR, null, "操作失败，邮箱已经存在！");
		}
		if (admin.getId() != null) {
			return modify(currentService, admin, "管理员");
		} else {
			admin.setUserType(Dictionary.USER_TYPE_ADMIN);
			// 加密密码
			admin.setAccountStatus(Dictionary.ACCOUNT_STATUS_ACTIVATE);
			try {
				admin.setPassword(EncrypUtil.getMD5(admin.getPassword()));
			} catch (UnsupportedEncodingException e) {
				e.printStackTrace();
			}
			admin.setRegisterTime(new Date());
			admin.setValid(1);
			return add(currentService, admin, "管理员");
		}
	}

	@RequestMapping(value = "/del")
	public @ResponseBody AjaxResult del(String keys) {
		return remove(currentService, keys);
	}

	@RequestMapping(value = "/editForm")
	public String editForm(Long id, HttpServletRequest request) {
		Users admin;
		List<Company> list = companyService.findByModel(null);
		request.setAttribute("list", list);
		if (id != null) {
			admin = currentService.findById(id);
			request.setAttribute("obj", admin);
		}
		return "super/admin-form";
	}
}